package oaSystem.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import oaSystem.biz.DepartmentBiz;
import oaSystem.entity.Department;
import oaSystem.web.security.Authorize;

@Controller
public class DepartmentController {
	
	@Autowired
	private DepartmentBiz departmentBiz;
	
	@Authorize(rules="department")
	@RequestMapping("/department")
	public String departmentList(Model model){
		List<Department> allDepartment = departmentBiz.findAllDepartment();
		model.addAttribute("allDepartment", allDepartment);
		return "department";
	}
	
	@Authorize(rules="department-info")	
	@RequestMapping(value="/department-info",method=RequestMethod.GET)
	public String departmentDetail(@RequestParam(value="id",defaultValue="0")int id,Model model){
		if(id>0){
			Department dept = departmentBiz.fetchDepartmentById(id);
			model.addAttribute("dept", dept);
		}
		return "department-edit";
	}
	
	@Authorize(rules="department-edit")
	@RequestMapping(value="/department-edit",method=RequestMethod.POST)
	public String editDepartment(Department department){
		departmentBiz.updaDepartment(department);
		return "redirect:/department";
	}
	
	@Authorize(rules="department-add")
	@RequestMapping(value="/department-add",method=RequestMethod.POST)
	public String addDepartment(Department department){
		department.setStatus(1);
		departmentBiz.addDepartment(department);
		return "redirect:/department";
	}
	
	@Authorize(rules="department-logout")
	@RequestMapping(value="/department-logout",method=RequestMethod.POST)
	public String logoutDepartment(int id){
		departmentBiz.logOutDepartment(id);
		return "redirect:/department";
	}
}
